﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBScript;
using eBlog.Common;
using eBlog.Common.Files;
using eBlog.Core;
using eBlog.Entity;
using eBlog.Service;
using eBlog.Common.Security;
namespace eBlog.Web.Users
{
    public partial class Register : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            IsLogin();//判断是否登录

            string model= Request.QueryString["action"];
            
            if(!string.IsNullOrEmpty(model))//来自表单的请求
            {
                if (model == "register")
                {
                    RegisterUser();
                }

            }
            else//不是表单的请求
            {
                Init_Template();
                TemplateUrl = "/Users/register.html";
                this.WriteHTML();
            }
         
        }

        //注册用户
        protected void RegisterUser()
        {
            var userName = Request.Form["User[username]"];
            var userEmail = Request.Form["User[email]"];
            var userPassword = Request.Form["User[password]"];
           if(!string.IsNullOrEmpty(userEmail)&&!string.IsNullOrEmpty(userPassword)&&!string.IsNullOrEmpty(userName))
           {
               IDBBase dal = DBFactory.GetDbProvider();
               User model = dal.From<User>().Where("(LoginName=@userName or Email=@userEmail)")
               .SetString("userEmail", userEmail)
               .SetString("userName", userName)
               .GetModel<User>();
               if(model==null)
               {
                   User user = new User();
                   user.Email = userEmail;
                   user.LoginName = userName;
                   user.PassWord = MD5.ADD(userPassword);
                   user.Enable = true;
                   user.CreateTime = DateTime.Now;
                   var isSuccess = UserService.AddUser(user);
                   if (isSuccess > 0)
                   {
                       //记录cookie
                       HttpCookie UserCookie = new HttpCookie("User");
                       UserCookie["id"] = isSuccess.ToString();
                       UserCookie["name"] = Server.UrlEncode(user.LoginName);
                       UserCookie["key"] = MD5.FormatCookie(user.LoginName);
                       UserCookie.Expires = DateTime.Now.AddMinutes(300);
                       Response.Cookies.Add(UserCookie);


                       Response.Redirect(ConfigHelper.SiteUrl);
                   }
               }
               else
               {
                   MessageBox.Show("用户名/邮箱已被注册!");
               }
              
           }
           else
           {
               MessageBox.Show("把资料输入全sa");
           }
            
           
        }

        /// <summary>
        /// 判断是否登录
        /// </summary>
        protected void IsLogin()
        {
            HttpCookie cookie = Request.Cookies["User"];

            
            if(cookie!=null)
            {
                Response.Redirect(ConfigHelper.SiteUrl);
            }

        }
    }
}